﻿package shooter
{
	import com.greensock.easing.Cubic;
	import com.greensock.TweenNano;
	import exey.utils.DrawUtils;
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * Gun skin, score textfields, start buttons, GameOver window
	 * @author Exey Panteleev
	 */
	public class GUI
	{
		[Embed(source = '../../assets/logo_eng.png')] static private const LOGO_CLASS:Class;
		[Embed(source = '../../assets/keys.png')] static private const KEYS_CLASS:Class;
		[Embed(source = '../../assets/stuff.png')] static private const WEAPON_CLASS:Class;
		
		private var weapon:Bitmap;		
		private var stage:Stage;
		public var scoreTF:TextField;
		public var stateTF:TextField;
		
		public var startWindow:Sprite;
		
		public var finalScoreTF:TextField;
		public var gameOverWindow:Sprite;
		private var restartButton:Sprite;
		private var goToMainMenuButton:Sprite;
		
		public var timerTF:TextField;
		
		private var game:Game;
		
		public function GUI( game:Game )
		{
			this.game = game;
			this.stage = game.stage;
			initialize();
		}
		
		private function initialize():void
		{
			// adding weapon
			weapon = new WEAPON_CLASS();
			game.addChild(weapon); 
			
			// adding score text
			var glowFilter:GlowFilter = new GlowFilter(0xFFFFFF, 1, 6, 6, 5, BitmapFilterQuality.LOW);
			scoreTF = new TextField();
			var format:TextFormat = new TextFormat("Verdana", 36, 0x000000, true);
			scoreTF.defaultTextFormat = format;
			scoreTF.autoSize = TextFieldAutoSize.LEFT;
			scoreTF.x = 530;
			scoreTF.filters = [ glowFilter ];
			game.addChild(scoreTF);
			
			// adding game over window
			gameOverWindow = createGameOverWindow();
			game.addChild(gameOverWindow);
			gameOverWindow.visible = false;
			
			// adding start window
			startWindow = createStartWindow();
			game.addChild(startWindow);
			
			// adding timer text
			timerTF = new TextField()
			format = new TextFormat("Verdana", 18, 0x000000, true);
			timerTF.defaultTextFormat = format;
			timerTF.filters = [ glowFilter ];
			timerTF.x = 300;
			game.addChild(timerTF);
		}
		
		/** Reset weapon position */
		public function resetWeapon():void
		{
			weapon.y = stage.stageHeight - weapon.height;
			weapon.filters = [];
		}

		/** Pull weapon down */
		public function pullWeapon(multiplier :int = 0):void
		{
			weapon.y += 2;
			if(multiplier) weapon.filters = [ new GlowFilter(0xFF9900, .2*multiplier, 2*multiplier, 2*multiplier, .75*multiplier) ]
		}
		
		public function showGameOverWindow():void
		{
			finalScoreTF.text = scoreTF.text;
			gameOverWindow.visible = true;
			gameOverWindow.cacheAsBitmap = true;
			gameOverWindow.scaleX = .1;
			gameOverWindow.scaleY = .1;
			TweenNano.killTweensOf(gameOverWindow);
			TweenNano.to(gameOverWindow, .4, { scaleX: 1, scaleY: 1, ease:Cubic.easeIn } );		
		}
		
		public function createGameOverWindow():Sprite
		{
			// drawing and adding background
			var s:Sprite = new Sprite();
			var g:Graphics;
			var glowFilter:GlowFilter = new GlowFilter(0x000000, 1, 6, 6, 5, BitmapFilterQuality.LOW)	
			DrawUtils.drawAdviceDogTemplate(s.graphics, .6, -150, -180);
			
			// adding monster
			var monster:Bitmap = new Bitmap(Monster.FIELD_RAT_BITMAP_DATA);
			monster.x = - monster.width * .5;
			monster.y = - monster.height * .7;
			s.addChild(monster);
			
			// adding top text
			var tf:TextField = new TextField();
			var f:TextFormat = new TextFormat("Verdana", 36, 0xFFFFFF, true);
			tf.defaultTextFormat = f;
			tf.text = "RATS";
			tf.selectable = false;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.x = - tf.width * .5;
			tf.y = -170;
			s.addChild(tf);
			tf.filters = [ glowFilter ];
			
			// adding score result
			finalScoreTF = new TextField();
			finalScoreTF.defaultTextFormat = f;
			finalScoreTF.autoSize = TextFieldAutoSize.LEFT;
			finalScoreTF.selectable = false;
			finalScoreTF.x = - finalScoreTF.width * .5 -20;		
			finalScoreTF.y = 15;
			s.addChild(finalScoreTF);
			finalScoreTF.filters = [ glowFilter ];
			
			// adding bottom text
			tf = new TextField();
			tf.defaultTextFormat = f;
			tf.text = "BURNED";
			tf.selectable = false;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.x = - tf.width * .5;
			tf.y = 70;
			s.addChild(tf);
			tf.filters = [ glowFilter ];
			
			// drawing and adding restart button
			restartButton = new Sprite();
			DrawUtils.drawTransparentTextButton(restartButton, "RESTART", 24);
			restartButton.addEventListener(MouseEvent.CLICK, onRestartClick)
			restartButton.y = 170;
			s.addChild(restartButton);
			
			goToMainMenuButton = new Sprite();
			DrawUtils.drawTransparentTextButton(goToMainMenuButton, "MAIN MENU", 24);
			goToMainMenuButton.addEventListener(MouseEvent.CLICK, onGoToMainMenuClick)
			goToMainMenuButton.y = 180+restartButton.height;
			s.addChild(goToMainMenuButton);
			
			s.x = stage.stageWidth * .5;
			s.y = stage.stageHeight * .5;

			return s;
		}
		
		private function onGoToMainMenuClick(e:MouseEvent):void 
		{
			game.goToMainMenu();
		}
		
		private function onRestartClick(e:MouseEvent):void
		{
			gameOverWindow.visible = false;
			game.restart();
		}
		
		public function createStartWindow():Sprite
		{
			// adding game logo
			var s:Sprite = new Sprite();
			var bitmap:Bitmap = new LOGO_CLASS();
			bitmap.x = - bitmap.width * .5;
			bitmap.y = -bitmap.height-50;
			s.addChild( bitmap );

			// adding keyboard help
			var keyboardHelp:Bitmap = new KEYS_CLASS();
			keyboardHelp.x = -135;
			keyboardHelp.y = -44;
			s.addChild(keyboardHelp);
			
			// drawing and adding start button
			var startButton:Sprite = new Sprite();
			DrawUtils.drawTransparentTextButton(startButton, "GO!", 48);
			startButton.addEventListener(MouseEvent.CLICK, onStartClick)
			startButton.y = 100;
			s.addChild(startButton);
			
			// set center position
			s.x = stage.stageWidth * .5;
			s.y = stage.stageHeight * .5;
			
			return s;
		}
			
		private function onStartClick(e:MouseEvent):void
		{
			game.removeChild(startWindow);
			game.restart()
		}

		public function resize():void 
		{
			weapon.x = stage.stageWidth / 2 - weapon.width / 2;
			weapon.y = stage.stageHeight - weapon.height;
			startWindow.x = gameOverWindow.x = stage.stageWidth / 2;
			startWindow.y = gameOverWindow.y = stage.stageHeight / 2;
			timerTF.x = stage.stageWidth / 2 - timerTF.width / 2;
			scoreTF.x = stage.stageWidth - 85;
		}
	}
}